#include <mainwindow.h>
#include <ui_mainwindow.h>
#include <QRect>
#include <QPixmap>
#include <QPainter>
#include <QFont>
#include <QKeyEvent>
#include <QMainWindow>
#include <cgamemap.h>
#include <roleman.h>
#include <monster.h>
#include <QMap>
#include <QMediaPlayer>
#include <QtMultimedia>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QMediaPlaylist>
#include <QSound>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QFont font;
    font.setFamily("Consolas");
    font.setPixelSize(16);
    QApplication::setFont(font);
    hero.HeroInit();
    setFixedSize(1080, 960);
    setWindowTitle("The Magic Tower");
    player=new QMediaPlayer;
    playlist = new QMediaPlaylist();
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
//    player->setMedia(QUrl("qrc:/new/prefix1/audio/bgm.wav"));

    playlist->addMedia(QUrl("qrc:/new/prefix1/audio/bgm.wav"));
    playlist->setPlaybackMode(QMediaPlaylist::Loop);

    player->setPlaylist(playlist);
    player->setVolume(30);  //音量
    player->play();
    //


}


MainWindow::~MainWindow()
{
    delete ui;
    player->stop();
    player->deleteLater();
    playlist->deleteLater();
}

void MainWindow::keyPressEvent(QKeyEvent *event){
    hero.HeroMove(map,event);
    update();
}


void MainWindow::paintEvent(QPaintEvent *){
    //画背景墙
    for(int i=0;i<18;i++)
    {
        for(int j=0;j<18;j++)
        {
            QRect target(200+i*48,20+j*48,48,48); //建立目标矩形，该区域是显示图像的目的地
            QRect source(0, 0, 48, 48); //建立源矩形，用来划定来自外部的源图像文件中需要显示的区域
            QPixmap image(":/new/prefix1/image/magictower.png"); //建立QImage类对象
            QPainter painter(this);
            painter.drawPixmap(target, image, source);
        }
    }



    DrawMap(map);

    ui->label_3->setText(QString::number(hero.GetHp()));
    ui->label_4->setText(QString::number(hero.GetLv()));
    ui->label_5->setText(QString::number(hero.GetAtt()));
    ui->label_6->setText(QString::number(hero.GetDef()));
    ui->label_7->setText(QString::number(hero.GetMoney()));
    ui->label_8->setText(QString::number(hero.GetExp()));
    ui->label_9->setText(QString::number(hero.GetYellowKey()));
    ui->label_10->setText(QString::number(hero.GetBlueKey()));
    ui->label_11->setText(QString::number(hero.GetRedKey()));
    ui->label_20->setText(QString::number(hero.GetFloor()+1));
}


void MainWindow::DrawMap(CGameMap &map){
    int level=hero.GetFloor();
    for(int i=0;i<18;i++)
    {
      for(int j=0;j<18;j++)
      {
        int p=map.m_Map[level][i][j];
        int py=p/10,px=p%10;
        QRect target(200+j*48, 20+i*48,48, 48); //建立目标矩形，该区域是显示图像的目的地
        QRect source(0+px*48, 0+py*48, 48, 48); //建立源矩形，用来划定来自外部的源图像文件中需要显示的区域
        QPixmap image(":/new/prefix1/image/magictower.png"); //建立QImage类对象
        QPainter painter(this);
        painter.drawPixmap(target, image, source);

      }
}

}


